home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CTOOLS10.ARJ / SSORT.C < prev    next >
C/C++ Source or Header  |  1991-12-31  |  3KB  |  104 lines

  1. /****************************************************************************
  2. *
  3. *                    Copyright (C) 1991 Kendall Bennett.
  4. *                            All rights reserved.
  5. *
  6. * Filename:        $RCSfile: ssort.c $
  7. * Version:        $Revision: 1.3 $
  8. *
  9. * Language:        ANSI C
  10. * Environment:    any
  11. *
  12. * Description:    Shell sort for array's and array's of pointers.
  13. *
  14. * $Id: ssort.c 1.3 91/12/31 19:40:39 kjb Exp $
  15. *
  16. * Revision History:
  17. * -----------------
  18. *
  19. * $Log:    ssort.c $
  20. * Revision 1.3  91/12/31  19:40:39  kjb
  21. * Modified include file directories
  22. * Revision 1.2  91/09/02  11:21:52  ROOT_DOS
  23. * Minor cosmetic change.
  24. * Revision 1.1  91/08/16  13:16:25  ROOT_DOS
  25. * Initial revision
  26. ****************************************************************************/
  27.  
  28. #include "debug.h"
  29. #include "ssort.h"
  30.  
  31. PUBLIC void ssort(char *base,int nel,int elsize,int (*cmp)(void *,void*) )
  32. /****************************************************************************
  33. *
  34. * Function:        ssort
  35. * Parameters:    base    - Pointer to base of array to sort
  36. *                nel        - Number of elements to sort
  37. *                elsize    - Size of elements in bytes
  38. *                cmp        - Comparision routine for elements
  39. *
  40. * Description:    Sorts the array using the standard "Shell Sort". The shell
  41. *                sort is simple and very efficient if we are sorting only
  42. *                small numbers of elements (say < 5000 elements).
  43. *
  44. ****************************************************************************/
  45. {
  46.     int        i,j;
  47.     int        gap,k,tmp;
  48.     char    *p1,*p2;
  49.  
  50.     for (gap = 1; gap <= nel; gap = 3*gap + 1);
  51.  
  52.     for(gap /= 3; gap > 0; gap /= 3)
  53.         for(i = gap; i < nel; i++)
  54.             for(j = i-gap; j >= 0; j -= gap) {
  55.                 p1 = base + (j * elsize);
  56.                 p2 = base + ((j+gap) * elsize);
  57.  
  58.                 if ((*cmp)(p1,p2) <= 0)        /* Compare the two elements    */
  59.                     break;
  60.  
  61.                 for (k = elsize; --k >= 0;) { /* Swap two elements, one    */
  62.                     tmp = *p1;
  63.                     *p1++ = *p2;
  64.                     *p2++ = tmp;
  65.                     }
  66.                 }
  67. }
  68.  
  69. PUBLIC void assort(void **base,int nel,int elsize,int (*cmp)(void*,void*) )
  70. /****************************************************************************
  71. *
  72. * Function:        assort
  73. * Parameters:    base    - Pointer to base of array to sort
  74. *                nel        - Number of elements to sort
  75. *                cmp        - Comparision routine for elements
  76. *
  77. * Description:    Same as ssort(), except that it sorts an array of pointer
  78. *                size objects.
  79. *
  80. ****************************************************************************/
  81. {
  82.     int        i,j,gap;
  83.     void    *tmp, **p1, **p2;
  84.  
  85.     for (gap = 1; gap <= nel; gap = 3*gap + 1);
  86.  
  87.     for(gap /= 3; gap > 0; gap /= 3)
  88.         for(i = gap; i < nel; i++)
  89.             for(j = i-gap; j >= 0; j -= gap) {
  90.                 p1 = base + j;
  91.                 p2 = base + j + gap;
  92.  
  93.                 if ((*cmp)(p1,p2) <= 0)        /* Compare the two elements    */
  94.                     break;
  95.  
  96.                 tmp = *p1;
  97.                 *p1 = *p2;
  98.                 *p2 = tmp;
  99.                 }
  100. }
  101.